﻿using System;
using SmartMotion.Core.Mgrs;

namespace SmartMotion.Core
{
    public class GetField : FieldBase
    {
        protected override ActionResult RunMethod()
        {
            try
            {
                Initilize();

                #region 读属性的对象

                Obj = null;
                if (!string.IsNullOrEmpty(ObjName))
                {
                    VarInfo varInfo = VarMgr.Ins.GetVarByGuid(ObjGuid);
                    Obj = varInfo.Value;
                }

                #endregion 读属性的对象

                var result = _fieldInfo.GetValue(Obj);

                #region 返回值赋值

                if (!ReturnType.Contains("Void") && !string.IsNullOrEmpty(ReturnObjName))
                {
                    var varinfo = VarMgr.Ins.GetVarByName(ReturnObjName);
                    if (varinfo != null) varinfo.Value = result;
                }

                #endregion 返回值赋值

                return ActionResult.Success;
            }
            catch (Exception e)
            {
                Em.GetEvent<DebugMessage>().Publish($"运行{ItemName}时出现异常:{e.Message}");
                return ActionResult.Error;
            }
        }
    }
}
